package org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.entity.Jieshouzhibiaodaibanbiao;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.entity.NianduguanjianZhibiaoDto;
import org.jeecg.modules.demo.jieshouzhibiaodaibanbiao.service.IJieshouzhibiaodaibanbiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.zhibiaobiao.dto.ZhibiaobiaoDto;
import org.jeecg.modules.demo.zhibiaobiao.entity.Zhibiaobiao;
import org.jeecg.modules.demo.zhibiaobiao.service.IZhibiaobiaoService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 接受指标待办表
 * @Author: jeecg-boot
 * @Date:   2025-09-24
 * @Version: V1.0
 */
@Tag(name="接受指标待办表")
@RestController
@RequestMapping("/jieshouzhibiaodaibanbiao/jieshouzhibiaodaibanbiao")
@Slf4j
public class JieshouzhibiaodaibanbiaoController extends JeecgController<Jieshouzhibiaodaibanbiao, IJieshouzhibiaodaibanbiaoService> {
	@Autowired
	private IJieshouzhibiaodaibanbiaoService jieshouzhibiaodaibanbiaoService;
	@Autowired
	private IZhibiaobiaoService zhibiaobiaoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param jieshouzhibiaodaibanbiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "接受指标待办表-分页列表查询")
//	@Operation(summary="接受指标待办表-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Jieshouzhibiaodaibanbiao>> queryPageList(Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Jieshouzhibiaodaibanbiao> queryWrapper = QueryGenerator.initQueryWrapper(jieshouzhibiaodaibanbiao, req.getParameterMap());
//		Page<Jieshouzhibiaodaibanbiao> page = new Page<Jieshouzhibiaodaibanbiao>(pageNo, pageSize);
//		IPage<Jieshouzhibiaodaibanbiao> pageList = jieshouzhibiaodaibanbiaoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}

	@GetMapping(value="/peiheList")
	public Result<IPage<NianduguanjianZhibiaoDto>> getPeiheList(){
		//获取当前登录用户的部门id
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departId = sysUser.getDepartIds();
		//去接收指标待办表中查配合部门的id
		Page<Jieshouzhibiaodaibanbiao> page = new Page<>();
		QueryWrapper<Jieshouzhibiaodaibanbiao> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("peihebumen",departId);
		queryWrapper.eq("zhuangtai","待处理");
		IPage<Jieshouzhibiaodaibanbiao> pageList = jieshouzhibiaodaibanbiaoService.page(page, queryWrapper);
		List<Jieshouzhibiaodaibanbiao> list = pageList.getRecords();
		IPage<NianduguanjianZhibiaoDto> dtoList = new Page<>();
		List<NianduguanjianZhibiaoDto> dto = new ArrayList<>();
		for (Jieshouzhibiaodaibanbiao j : list) {
			NianduguanjianZhibiaoDto d = new NianduguanjianZhibiaoDto();
			d.setId(j.getId());
			Zhibiaobiao z = zhibiaobiaoService.getById(j.getZhibiaoid());
			d.setGonzuoneirong(z.getKaohezhibiao());
			d.setZhiliangyaoqiu(z.getZhibiaoyaoqiu());
			d.setGuikoubumen(z.getZerenbumen());
			dto.add(d);
		}
		dtoList.setRecords(dto);
		return Result.OK(dtoList);
	}
	
	/**
	 *   添加
	 *
	 * @param jieshouzhibiaodaibanbiao
	 * @return
	 */
	@AutoLog(value = "接受指标待办表-添加")
	@Operation(summary="接受指标待办表-添加")
	@RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao) {
		jieshouzhibiaodaibanbiaoService.save(jieshouzhibiaodaibanbiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jieshouzhibiaodaibanbiao
	 * @return
	 */
	@AutoLog(value = "接受指标待办表-编辑")
	@Operation(summary="接受指标待办表-编辑")
	@RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao) {
		jieshouzhibiaodaibanbiaoService.updateById(jieshouzhibiaodaibanbiao);
		return Result.OK("编辑成功!");
	}

	//接受
//	@PostMapping("/accept")
//	public Result<String> accept(@RequestBody Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao){
//		//根据前端传回的id找到对应的记录，修改状态为“已接受”，并保存
//		Jieshouzhibiaodaibanbiao js=jieshouzhibiaodaibanbiaoService.getById(jieshouzhibiaodaibanbiao.getId());
//		System.out.println("id:"+ jieshouzhibiaodaibanbiao.getId());
//		js.setZhuangtai("已接受");
//		jieshouzhibiaodaibanbiaoService.updateById(js);
//		return Result.OK("接收成功");
//	}
//
//	//驳回任务
//	@PostMapping("/refuse")
//	public Result<String> refuse(@RequestBody Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao){
//		//根据前端传回的id找到对应的记录，修改状态为“已驳回”，并保存，将配合部门的id存入指标表驳回情况字段（dto接收）
//		Jieshouzhibiaodaibanbiao js=jieshouzhibiaodaibanbiaoService.getById(jieshouzhibiaodaibanbiao.getId());
//		js.setZhuangtai("已驳回");
//		jieshouzhibiaodaibanbiaoService.updateById(js);
//		return Result.OK("驳回成功");
//	}

	//接收驳回
	 @PostMapping("/receive")
	 public Result<String> receive(@RequestBody NianduguanjianZhibiaoDto niandu){
		Jieshouzhibiaodaibanbiao js=jieshouzhibiaodaibanbiaoService.getById(niandu.getId());
		if(niandu.getZhuangtai().equals("已接受")){
			js.setZhuangtai("已接收");
			jieshouzhibiaodaibanbiaoService.updateById(js);
			return Result.OK("接收成功");
		} else if (niandu.getZhuangtai().equals("已驳回")) {
			js.setZhuangtai("已驳回");
			jieshouzhibiaodaibanbiaoService.updateById(js);
			return Result.OK("驳回成功");
		}else{
			return Result.error("操作错误");
		}
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "接受指标待办表-通过id删除")
	@Operation(summary="接受指标待办表-通过id删除")
	@RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jieshouzhibiaodaibanbiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "接受指标待办表-批量删除")
	@Operation(summary="接受指标待办表-批量删除")
	@RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jieshouzhibiaodaibanbiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "接受指标待办表-通过id查询")
	@Operation(summary="接受指标待办表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Jieshouzhibiaodaibanbiao> queryById(@RequestParam(name="id",required=true) String id) {
		Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao = jieshouzhibiaodaibanbiaoService.getById(id);
		if(jieshouzhibiaodaibanbiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jieshouzhibiaodaibanbiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jieshouzhibiaodaibanbiao
    */
    @RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Jieshouzhibiaodaibanbiao jieshouzhibiaodaibanbiao) {
        return super.exportXls(request, jieshouzhibiaodaibanbiao, Jieshouzhibiaodaibanbiao.class, "接受指标待办表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("jieshouzhibiaodaibanbiao:x_jieshouzhibiaodaibanbiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Jieshouzhibiaodaibanbiao.class);
    }

}
